﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.UnitOfWork;
using Core.Repository;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.Data;

namespace PersonalHealth.Data.Repository
{
    public class PersonalDataThresholdRepository : Repository<PersonalDataThreshold>, IPersonalDataThresholdRepository
    {
        public PersonalDataThresholdRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {

        }

        #region IPersonalDataThresholdRepository 成员

        public IDictionary<string, PersonalDataThreshold> GetThreshold(string measureType, int userId)
        {
            var context = UnitOfWork.Context as HealthContext;
            var q =(from t in context.MeasureTypes
                        join st in context.MeasureSubTypes on t.Id equals st.MeasureTypeId
                        join ps in context.PersonalDataThresholds on st.Id equals ps.MeasureSubTypeId
                        join u in context.Users on ps.UserId equals u.Id
                        where t.Code == measureType && u.Id == userId
                        select ps).Distinct();
            IDictionary<string, PersonalDataThreshold> dict = new Dictionary<string, PersonalDataThreshold>();
            foreach (var i in q)
            {
                dict.Add(i.MeasureSubType.Code, i);
            }
            return dict;
        }

        #endregion
    }
}
